package org.yankun.exam4j.core.paper.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.yankun.exam4j.common.daobase.DaoBase;
import org.yankun.exam4j.core.paper.service.PaperService;
import org.yankun.exam4j.entity.Paper;

@Transactional
@Service
public class PaperServiceImpl implements PaperService {
	
	@Resource
	private DaoBase<Paper> dao;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Paper> getPapersByExamId(Serializable examId) {
		String hql = "from Paper where exam.uid = ?";
		return dao.getHibernateTemplate().find(hql, examId);
	}

	@Override
	public Paper getPaperById(Serializable paperId) {
		return dao.getHibernateTemplate().get(Paper.class, paperId);
	}


	@Override
	public void addPaper(Paper paper) {
		Assert.notNull(paper, "Paper must not be null");
		dao.getHibernateTemplate().save(paper);
	}

	@Override
	public List<Paper> getPapersByUser(Serializable userId) {
		StringBuilder hql = new StringBuilder();
		List<Object> values = new ArrayList<Object>();
		
		hql.append("from Paper where createUserId = ?");
		values.add(userId);
		
		hql.append(" order by createTime desc ");
		
		return dao.hql(hql.toString(), values.toArray());
	}

	
}
